package com.example.hellonotes.activity;


import com.example.hellonotes.BaseActivity;

import com.example.hellonotes.R;
import com.example.hellonotes.R.id;
import com.example.hellonotes.R.layout;
import com.example.hellonotes.contants.AccessTokenKeeper;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.c.i;

/**
 * 
 * 程序的入口  判断缓存中用户是否登录 进行跳转 主界面或者登录界面
 *
 */
public class SplashActivity extends BaseActivity{

	private static final int WHAT_INTENT2LOGIN = 1;
	private static final int WHAT_INTENT2MAIN = 2;
	private static final long SPLASH_DUR_TIME = 1000;
	private static final long DELAY_TIME = 2000L;
	
	private Oauth2AccessToken accessToken;
	
/*	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			
			switch (msg.what) {
			case WHAT_INTENT2LOGIN:
				intent2Activity(loginActivity.class);
				finish();
				break;
			case WHAT_INTENT2MAIN:
				intent2Activity(MainActivity.class);
				finish();
				break;
			default:
				break;
			}
		}
		
	};*/
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		BmobUser bmobUser = BmobUser.getCurrentUser(this);
		accessToken = AccessTokenKeeper.readAccessToken(this);
		if(bmobUser != null || accessToken.isSessionValid()){
			redirectByTime();
		}else{
			redirectByTime2();
		}
	}
	
	
	/**
	 * 根据时间进行页面跳转
	 */
	private void redirectByTime() {
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				intent2Activity(MainActivity.class);
				finish();
			}
		}, DELAY_TIME);
	}
	
	private void redirectByTime2() {
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				intent2Activity(loginActivity.class);
				finish();
			}
		}, DELAY_TIME);
	}
	
}
